package com.ruipengkj.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TStockAlarm;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.service.TEmployeeService;

public class StatementInventoryWarningModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Vector<List<Object>> rowdatas=new Vector<List<Object>>();
	private Vector<String> columns = new Vector<String>();
	
	TEmployeeService tEmployeeService;
	public StatementInventoryWarningModel(){
		tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		columns.add("编号");
		columns.add("商品编号");
		columns.add("商品名称");
		columns.add("发生时间");
		columns.add("最低伐值");
		columns.add("最高伐值");
		columns.add("操作人");
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch(columnIndex) {
		case 4:
		case 5:
			return Double.class;
		}
		return super.getColumnClass(columnIndex);
	}
	//获得行数
	public int getRowCount(){
		return rowdatas.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		return ((Vector)rowdatas.get(row)).get(column);
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//获取列对应的类型
//	public Class getColumnClass(int column){
//		return column == (columns.size()-1) ? ImageIcon.class : Object.class;
//	}
	//设置单元格是否可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	public void removeData(int row){
		rowdatas.remove(row);
	}
	
	
	
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		//取出对应的记录
		Vector rowdata = (Vector)rowdatas.get(rowIndex);
		//删除原来的值
		rowdata.remove(columnIndex);
		//添加用户新修改的值
		rowdata.add(columnIndex,aValue);
	}

	public void setRowData(List<TStockAlarm> stockAlarms){
		if(stockAlarms!=null&&stockAlarms.size()!=0){
			rowdatas.clear();
			if(stockAlarms!=null&&stockAlarms.size()!=0){
				for(int i=0;i<stockAlarms.size();i++){
					TStockAlarm stockAlarm=stockAlarms.get(i);
					Vector<Object> obj=new Vector<Object>();
					obj.add(stockAlarm.getAlarmID());
					TCommodity tCommodity=stockAlarm.getTCommodity();
					if (tCommodity != null){
						obj.add(tCommodity.getCommodityID());
						obj.add(tCommodity.getName());
					}else{
						obj.add("");
						obj.add("");
					}
					obj.add(stockAlarm.getHappenTime());
					obj.add(stockAlarm.getMinWalve());
					obj.add(stockAlarm.getMaxWalve());
					try {
						TEmployee tEmployee = tEmployeeService.getNameByIdService(stockAlarm.getOperator());
						if (tEmployee != null && tEmployee.getName() != null)
							obj.add(tEmployee.getName());
						else
							obj.add("");
					} catch (Exception e) {
						e.printStackTrace();
					}
					rowdatas.add(obj);
				}
			}
		}
	}

	public void clear() {
		rowdatas.clear();		
	}
}
